<?php
declare(strict_types=1);
/**
 * Fastapp.
 * 4/26/24
 * @Author deepsea
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class NullFilterMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $params = array_filter(array_merge($request->getQueryParams(), $request->getParsedBody() ?? []), function ($value) {
            return $value !== null;
        });
        foreach ($params as $k => $v) {
            if (is_string($v) && !is_numeric($v)) $params[$k] = trim($v);
        }
        return $handler->handle($request->withParsedBody($params));
    }
}